<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\we\model;

use think\Model;

class WeFansBuy extends Model
{
    protected $insert = ['status'=>1]; 
    
    /**
     * 获取微信配置信息，需要缓存
     */
    public function info($openid,$field=true){
      
      if (is_numeric($openid)){
        $map['id']=$openid;
      }else{
        $map['openid']=$openid;
      }
      $fans = $this->field($field)->where($map)->find();
     
      return  $fans;
    }

     public function infoForFans($openid,$field=true){
      $fans=$this->info($openid,$field=true);
      if (!$fans) return false;
      $printer=model('printer/PrinterFans')->info($fans['openid'],'balance');

      $message="粉丝信息";
      $message.="\nID：".$fans['id'];
      $message.="\n昵称：".$fans['nickname'];
      if($printer) $message.="\n打印余额：".$printer['balance'];
      return  ['fans'=>$fans,'message'=>$message];
    }

    public function infoForFansByOrder($data){
     $fans = $this->infoForFans($data['before']);
     if (!$fans) return ['message'=>'粉丝不存在，请核对粉丝ID'];
     return ['message'=>$fans['message']]; //空尾巴查询
    }

     public function getAdmins($appid){
      $map['appid']=$appid;
      $map['userid']=['NEQ',''];
     
      $admins=$this->where($map)->select();

      
      return  $admins;
    }




    public function editData($data){

      
      if (isset($data['id'])){
         $res = $this->allowField(true)->where('id',$data['id'])->update($data);
      }else{
         $res = $this->allowField(true)->data($data)->save();
      }

      return $res;
    }

    public function buy($data){
  
        if(strstr($data['before'],'+')){
            $fans_id= substr($data['before'],0,strrpos($data['before'],'+'));
            $customer=str_replace($fans_id.'+','',$data['before']);
        }
        $fans=model('we/WeFans')->info($fans_id);
       
        if (!$fans)return ['message'=>$fans_id.'号粉丝不存在'];
        if(strstr($data['back'],'++')){
            $desk = substr($data['back'],0,strrpos($data['back'],'++'));
            $price=str_replace($desk.'++','',$data['back']);
          }else{
            $desk = substr($data['back'],0,strrpos($data['back'],'+'));
            $price=str_replace($desk.'+','',$data['back']);
        }

        //数据有效性验证
        if (!$desk ) return ['message'=>'桌号没有输入'];
        if (!$price) return ['message'=>'消费金额没有输入'];
          
        $buy=[];

        

         
         //todo 防止重复录入
         $map['aid']=$data['aid'];
         $map['appid']=$data['appid'];
         $map['openid']=$fans['openid'];
         $map['desk']=$desk;
         $map['price']=$price;
         $have=$this->where($map)->whereTime('create_time','today')->find();
         if ($have)return '系統檢測到重複錄入，請檢查';
        


         //积分
        if(strstr($data['back'],'++')){
          $score_message=model('we/WeFansCfo')->cfoScore($fans,1,$data);
          if ($score_message)$buy['score']=1;
        }

        //分销提成
        if($fans['subscribe_openid'] and $data['options']['tui_reward']){
         
          $father_fans=model('we/WeFans')->info($fans['subscribe_openid']);
          $reward=$data['options']['tui_reward']*$price;
          $amount_message=model('we/WeFansCfo')->cfoAmount($father_fans,$reward,$data,'推广消费',$father_fans['nickname'].'您的好友'.$fans['nickname'].'消费'.$price.'元，您获得了'.$reward.'元奖励，已经存入您的会员账户下，请查收！');
          if ($amount_message){
            $buy['reward']=$reward;
            $buy['reward_openid']=$fans['subscribe_openid'];
          }
        }
        
        //老顾客
         $last_scan=model('we/WeSceneScan')->getLastAdmin($fans['openid']);
         if ($last_scan ){
          $admin=model('admin/AdminUser')->info($last_scan['admin']);
          $shop=model('admin/AdminShop')->info($admin['shopid']);
          $buy['last_admin']=$last_scan['admin'];
          //奖励储值
          $last_reward=$shop['last_reward'];
          $amount_message=model('admin/AdminUser')->cfoAmount($admin,$last_reward,$admin['name'].'您的顾客'.$fans['nickname'].'再次进店消费'.$price.'元，您获得了'.$last_reward.'元奖励，已经存入您的员工账户下，请查收！');
          $buy['last_reward']=$reward;
         }
         
         $buy['aid']=$data['aid'];
         $buy['appid']=$data['appid'];
         $buy['shopid']=$data['fans']['admin_info']['shopid'];
         $buy['fans']=$fans['id'];
         $buy['openid']=$fans['openid'];
         $buy['nickname']=$fans['nickname'];
         $buy['admin']=$data['admin'];
         $buy['name']=$data['fans']['admin_info']['name'];
         $buy['desk']=$desk;
         $buy['customer']=$customer;
         $buy['price']=$price;
         $res=$this->editData($buy);




         if ($res) $message=$fans['id'].'号顾客'.$fans['nickname'].'带'.$customer.'人在'.$desk.'号桌消费'.$price.'元，录入成功'.(isset($buy['score'])?'，已为顾客积分！':'');
         return $message;
    }

    
  
    

    
}